﻿using MyClassLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProgram
{
    public partial class FrmExamEdit : Form
    {
        public FrmExamEdit()
        {
            InitializeComponent();
        }
        MyDB.MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();
        private void FrmExamEdit_Load(object sender, EventArgs e)
        {
            BindingSource bsFrequency = new BindingSource();
            bsFrequency.DataSource = MyClassLibrary.frequency.GetFrequency();
            comboBox4.DataSource = bsFrequency;
            comboBox4.DisplayMember = "fName";

            comboBox3.DataSource = MyClassLibrary.Exam.GetExam();
            comboBox3.DisplayMember = "eName";

            var q=DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First();
            comboBox3.SelectedIndex =(int) DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().檢驗項目ID - 1;
            comboBox4.SelectedIndex = (int)DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().檢驗頻率ID - 1;
            起始日期DateTimePicker.Value = q.起始日期;
            結束日期DateTimePicker.Value = (DateTime)q.結束日期;
            備註TextBox.Text = q.備註;
            結案原因TextBox.Text = q.結案原因;
            結案CheckBox.Checked = (bool)q.結案;
            
            

        }

        private void button1_Click(object sender, EventArgs e)
        {            
            MyDB.ExamSchedule i = new MyDB.ExamSchedule
            {                
                檢驗項目ID = ((Exam)comboBox3.SelectedItem).eID,
                檢驗頻率ID = ((frequency)comboBox4.SelectedItem).fID,
                起始日期 = 起始日期DateTimePicker.Value,
                結束日期 = 結束日期DateTimePicker.Value,
                備註 = 備註TextBox.Text,
                結案原因 = 結案原因TextBox.Text,
                結案 = 結案CheckBox.Checked
            };
            
                try
                {
                    DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().起始日期 = i.起始日期;
                    DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().備註 = i.備註;
                    DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().結束日期 = i.結束日期;
                    DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().結案 = i.結案;
                    DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().結案原因 = i.結案原因;
                    DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().檢驗項目ID = i.檢驗項目ID;
                    DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().檢驗頻率ID = i.檢驗頻率ID;
                    DB.SubmitChanges();

                    MessageBox.Show("修改成功!");
                    this.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("修改失敗");
                }

                ClsRecord.Record(sender, this.Name, DB.ExamSchedules.Where(n => n.排程ID == ClsExamSchedule.id).First().Diagnosis.RegisterInformation.Patients.病患ID);
            }
        

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
